class AdGroupCreativesController < ApplicationController
  before_action :set_ad_group_creative, only: [:destroy]
  def index
  @crtSizeMediumMenuId = params[:crtSizeMediumMenuId] == '0' ? 0 : params[:crtSizeMediumMenuId].to_i
  if @crtSizeMediumMenuId.nil? || @crtSizeMediumMenuId == 0
    @ad_group_creatives = current_user.ad_group_creatives.paginate(page: params[:page]).order(created_at: :desc)
  else
    @ad_group_creatives = current_user.ad_group_creatives.where(crt_size_medium_menu_id: @crtSizeMediumMenuId).paginate(page: params[:page]).order(created_at: :desc)
  end
  @crt_size_medium_menus = CrtSizeMediumMenu.all

  end


  def creatives
    @ad_group_creatives = AdGroupCreative.where(id: params[:ids])
=begin
    respond_to do |format|
        format.json { render json: @ad_group_creative }
    end
=end
    render json: @ad_group_creatives
  end


  def new
    @crt_size_medium_menus = CrtSizeMediumMenu.all
    render layout: false
  end

  def create
    @ad_group_creative = AdGroupCreative.create(ad_group_creative_params)
    @ad_group_creative.user_id = session[:user_id]

    respond_to do |format|
      if @ad_group_creative.save
        format.html { render :show }
        format.json { render json: @ad_group_creative }
      else
        format.html { render :show }
        format.json { render json: @ad_group_creative }
      end
      end

  end


  def destroy
    if @adGroupCreative.destroy
      flash[:success] = "删除成功"
      redirect_to request.referrer || ad_group_creatives_path
    else
      flash[:error] = "有广告在使用无法删除"
      redirect_to request.referrer || ad_group_creatives_path
    end

    #AdGroupCreative.delete_all

  end

  def list
    @crt_size_medium_menus = CrtSizeMediumMenu.all
    @ad_group_creatives =current_user.ad_group_creatives.where(siteset_type: params[:siteset_type])
    render layout: false
  end

  private
  def ad_group_creative_params
    params.require(:ad_group_creative).permit(:creative_name, :campaign_id, :ad_group_id, :crt_size_medium_menu_id,
                                              :crt_size_detail,:banner_show_path1,:banner_show_path2,:banner_show_path3,:banner_path1,:banner_path2,:banner_path3,:share_title,:share_describe,
    :share_path,:share_show_path,:siteset_type,:creative_copywriter,:trademark_path,:trademark_name,:trademark_show_path)
  end

  def set_ad_group_creative
    @adGroupCreative = AdGroupCreative.find(params[:id])
  end

end
